//
//  WBTabBar.swift
//  WeiBo
//
//  Created by chenguanglei on 16/11/25.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class WBTabBar: UITabBar {

    var closure: (()->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        addSubview(composeButton)
    }
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let itemW:CGFloat = self.bounds.size.width/5
        
        let itemH:CGFloat = self.bounds.size.height
        
        var index : Int = 0
        
        for subview in subviews {
            
        if subview.isKind(of:NSClassFromString("UITabBarButton")!) {
                
                subview.frame = CGRect.init(x: CGFloat(index) * itemW, y: 0, width: itemW, height: itemH)
            
            index == 1 ? (index = index+2) : (index = index+1)
                
                
            }
            
            
            composeButton.frame = CGRect.init(x: 2*itemW, y: 0, width: itemW, height: itemH)
            
            
        }
        
        
        
        
    }
    
    
    private lazy var composeButton:UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: #selector(composeButtonClick), for: UIControlEvents.touchUpInside)
        
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        
        
        
        
        return button
        
    }()
    
    
    
    @objc private func composeButtonClick () {
    
        closure?()
    
    }
    

}
